package com.goverse.mancraft.man;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.goverse.mancraft.man.action.AbstractAction;
import com.goverse.mancraft.util.IDGenerator;
import com.goverse.mancraft.world.Location;
import com.goverse.mancraft.world.Time;
import com.goverse.mancraft.world.WorldTimer;

/**
 * Created by gaoyu on 2017/4/15.
 */

public class Man implements WorldTimer.OnTimePassListener{

    public Man() {
        mId = IDGenerator.generateId();
        WorldTimer.getInstance().addOnTimePassListener(this);
    }

    public Man(String name) {
        mName = name;
        mId = IDGenerator.generateId();
        WorldTimer.getInstance().addOnTimePassListener(this);
    }

    @Override
    public void onTimePassed(Time time) {
        minusHp(getCurrAction().getLoseHp());
    }

    public interface IManHealthListener {
        public void onManLowHp(Man man, int hp);
        public void onManDied(Man man);
    }

    private AbstractAction mCurrAction;

    public AbstractAction getCurrAction() {
        return mCurrAction;
    }

    public void setCurrAction(AbstractAction currAction) {
        this.mCurrAction = currAction;
    }

    private Location mCurrLocation = new Location(0, 0);

    public Location getmCurrLocation() {
        return mCurrLocation;
    }

    public void setmCurrLocation(Location mCurrLocation) {
        this.mCurrLocation = mCurrLocation;
    }

    private IManHealthListener mManHealthListener;

    public void setManHealthListener(IManHealthListener manHealthListener) {
        mManHealthListener = manHealthListener;
    }
    private String mId;

    private int mMoney;

    public int getMoney() {
        return mMoney;
    }

    public void setMoney(int money) {
        this.mMoney = money;
    }

    public void addMoney(int money) {
        setMoney(getMoney() + money);
    }

    private int mHp = 100;

    private String mName;

    private int mLowHp = 60;

    private int mVelocity = 10;

    private boolean mIsSleep = false;

    public void setIsSleep(boolean isSleep) {
        mIsSleep = isSleep;
    }

    public boolean isSleeping() {return mIsSleep;}

    public void setVelocity(int velocity) {
        mVelocity = velocity;
    }

    public int getVelocity() {
        return mVelocity;
    }

    public int getLowHp() {
        return mLowHp;
    }

    public void setLowHp(int lowHp) {
        this.mLowHp = lowHp;
    }

    public String getId() {
        return mId;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        this.mName = name;
    }

    public int getHp() {
        return mHp;
    }

    private void setHp(int hp) {
        if (hp >= 100) this.mHp = 100;
        else if (hp <= 0) this.mHp = 0;
        else this.mHp = hp;
        if (mHp <= getLowHp()) {
            if (mManHealthListener != null) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mManHealthListener.onManLowHp(Man.this, getLowHp());
                    }
                });
            }
        }
        if (mHp == 0) {
            if (mManHealthListener != null) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mManHealthListener.onManDied(Man.this);
                    }
                });

            }
            WorldTimer.getInstance().removeOnTimePassListener(this);
        }
        Log.d("ManCraft", mName + "---HP:" + mHp + "---RemainMoney:---" + mMoney + "---CURRLOCATION---(" + getmCurrLocation().getX() + "," + getmCurrLocation().getY() + ")");
    }

    public void addHp(int hp) {

        setHp(getHp() + hp);
    }

    public void minusHp(int hp) {
        setHp(getHp() - hp);
    }
}
